const {Sequelize, Model} = require('sequelize')
const database = require('../config/config').database

const {unset, clone} = require('lodash')

const sequelize = new Sequelize(database.dbName, database.user, database.password, {
  host: database.host,
  dialect: database.dialect,
  port: database.port,
  logging: true,
  timezone: '+08:00',
  define: {
    // create_time && update_time
    timestamps: true,
    // delete_time
    paranoid: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    // 把驼峰命名转换为下划线
    underscored: true,
    freezeTableName: true,
    scopes: {
        bh: {
            attributes: {
                exclude: ['updated_at', 'deleted_at', 'created_at']
            }
        }
    }
  }
})

// 创建模型
sequelize.sync({
  force: false
})

/**
 * @team:目标实现，在序列化的时候不包含时间字段
 */
// Model.prototype.toJSON = function() {
//   let data = clone(this.dataValues)

//   unset(data, 'created_at')
//   unset(data, 'updated_at')
//   unset(data, 'deleted_at')
//   return data;
  
// }

module.exports = {
  sequelize
}